﻿using LocoyApp.Locoy.Entry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LocoyApp.Locoy
{
    public class SoftTitleBuilder : LocoyCode
    {
        /// <summary>
        /// 执行方法，不能修改类和方法名称。
        /// </summary>
        /// <param name="content">标签内容</param>
        /// <param name="response">页面响应，包含了Url、原始Html等属性</param>
        /// <returns>返回处理后的标签内容</returns>
        public string Run(string content, ResponseEntry response)
        {
            var text = content;
            // 网站名称
            var appName = Filter(text);
            var versionName = GetRandVersionName();
            var version = string.Format("v{0}.{1}", new Random().Next(0, 10), new Random().NextDouble().ToString("###"));
            //var title1 = Combination(appName, versionName);
            //var title2 = Combination(title1, version);

            //在这里编写处理代码
            return content;
        }

        private string Filter(string text)
        {
            var versionNames = GetVersionNames();

            foreach (var version in versionNames)
            {
                text = text.Replace(version, "");
            }

            return text;
        }

        private string Combination(string text1, string text2, string text3, string separator = "")
        {
            return string.Format("{0}{1}{2}{3}{4}", text1, separator, text2, separator, text3);
        }

        private List<string> GetVersionNames()
        {
            return new List<string>
            {
                "主机版","电脑版","安卓版","IOS版","苹果版","iphone版","PC版","移动版","重制版",
                "扩展版","无限币","正版","正式版","单机版","无敌版","中文版","联机版","双人版",
                "完整版","手机版","汉化版","原版","破解版","网页版","Flash版","怀旧版","经典版",
                "Q版","卡通版","完全版","重制版","绿色版","免安装版","精简版","女生版","女神版",
                "金手指版","Alpha版","内测版","Beta版","Demo版","演示版","Enhanced版","增强版",
                "加强版","Free版","自由版","Full Version版","完全版","Shareware版","共享版",
                "Release版","发行版","Upgrade版","升级版","Retail版","零售版","Plus版","Preview版",
                "预览版","Standard版","标准版","超值版","专业版","简易版","豪华版","注册版","英文版",
                "多语言版","繁体中文版","公测版","内测版","测试版","特别版","试用版"
            };
        }

        private string GetRandVersionName()
        {
            var versionNames = GetVersionNames();

            return Rand(versionNames);
        }

        private string StringFormat(string text1, string text2)
        {
            var segment = Shuffle("111111222223333444556");
            var titleFormat = "";
            switch (segment)
            {
                case "2":
                    titleFormat = "{0}({1})";
                    break;
                case "3":
                    titleFormat = "「{0}」{1}";
                    break;
                case "4":
                    titleFormat = "{0}，{1}";
                    break;
                case "5":
                    titleFormat = "〈{0}〉{1}";
                    break;
                case "6":
                    titleFormat = "{0}|{1}";
                    break;
                case "1":
                default:
                    titleFormat = "{0}_{1}";
                    break;
            }

            return string.Format(titleFormat, text1, text2);
        }

        private string Shuffle(string input)
        {
            Random random = new Random();
            char[] cArr = input.ToCharArray();
            string result = "";
            for (int i = input.Length - 1; i >= 0; i--)
            {
                int index = random.Next(0, i);
                result += cArr[index];
                cArr[index] = cArr[i];
            }

            return result.Substring(0, 1);
        }

        public T Rand<T>(List<T> source)
        {
            var random = new Random();
            var newList = new List<T>();
            foreach (var item in source)
            {
                newList.Insert(random.Next(newList.Count), item);
            }
            for (int i = 0; i < source.Count; i++)
            {
                source[i] = newList[i];
            }

            return source.First();
        }
    }
}
